﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Globalization;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework.Media;
using Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;
using System.IO;
using System.Threading;

namespace WidgetLibrary
{
    /// <summary>
    /// Implements IWidget.
    /// </summary>
    class Widget : IWidget
    {

        #region Properties & Constructor

        private WebBrowser _widget;

        public Widget(WebBrowser widget) 
        {
            this._widget = widget;
        }

        #endregion

        /// <summary>
        /// Get language of device. 
        /// Work with navigator.userLanguage inside of widget. Is not neccessary
        /// </summary>
        public void GetLanguage() { }


        /// <summary>
        /// Save configuration from widget to Isolated Storage
        /// </summary>
        /// <param name="configuration"> Deserialized string with configuration </param>
        public void SaveConfiguration(string configuration) 
        {
            Configuration config = new Configuration();
            config.SaveConfiguration(configuration);
        }


        /// <summary>
        /// Load configuration from Isolated Storage to widget
        /// </summary>
        public void LoadConfiguration()
        {
            Configuration config = new Configuration();
            config.LoadConfiguration(this._widget);
        }


        /// <summary>
        /// Launch a new WebBrowser task
        /// </summary>
        /// <param name="url"> url to navigate </param>
        public void OpenURL(string url)
        {
            WebBrowserTask w = new WebBrowserTask();
            w.URL = url;
            w.Show();
        }


        /// <summary>
        /// Download a file. 
        /// If is an Image, this save into the Media Library.
        /// Else, Navigate to url
        /// </summary>
        /// <param name="url"> url to download the file</param>
        public void DownloadFile(string url)
        {
            Downloader downloader = new WidgetLibrary.Downloader(url);
            downloader.Download();   
        }        


        /// <summary>
        /// Navigate to previous page of the widget
        /// </summary>
        public void Back()
        {
            this._widget.InvokeScript("eval", new string [] {"history.go(-1)"});
        }


        /// <summary>
        /// Show a notification from widget
        /// </summary>
        /// <param name="text"> text to show</param>
        public void ShowNotification(string text)
        {
            MessageBox.Show(text);
        }


        /// <summary>
        /// Play a sound effect
        /// </summary>
        /// <param name="path">path of the audio resource</param>
        /// <param name="looping">"true" if is a looping sound. "false" otherwise</param>
        public void PlayAudio(string path, string looping)
        {
            SoundManager.PlayAudio(path, looping);
        }


        /// <summary>
        /// Stop all audio effects
        /// </summary>
        public void StopAudio()
        {
            SoundManager.StopAudio();
        }


        /// <summary>
        /// Play a music background
        /// </summary>
        /// <param name="musicSource">path of the file</param>
        public void PlayMusicBackground(string musicSource)
        {
            SoundManager.PlayMusicBackground(musicSource);
        }

        /// <summary>
        /// Stop a music background
        /// </summary>
        public void StopMusicBackground()
        {
            SoundManager.StopMusicBackground();
        }


        /// <summary>
        /// Play a video
        /// </summary>
        /// <param name="url">video url</param>
        public void PlayVideo(string url)
        {
            MediaPlayerLauncher m = new MediaPlayerLauncher();
            m.Media = new Uri(url, UriKind.RelativeOrAbsolute);
            m.Controls = MediaPlaybackControls.All;
            m.Show();
        }


    }
}
